package com.xu.sys.servlet;

import com.xu.sys.bean.SysMenu;
import com.xu.sys.bean.SysRole;
import com.xu.sys.service.IMenuService;
import com.xu.sys.service.impl.MenuServiceimpl;
import com.xu.sys.utils.StringUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(name = "menuServlet",urlPatterns = {"/sys/menuServlet"})
public class MenuServlet extends BaseServlet{
    //* 菜单功能不做分页
    //* @param req
    //* @param resp
    //* @throws Exception
    private IMenuService service = new MenuServiceimpl();

    @Override
    public void list(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        List<SysMenu> list = service.list(null);
        req.setAttribute("list",list);
        req.getRequestDispatcher("/sys/menu/list.jsp").forward(req,resp);

    }

    @Override
    public void saveOrUpdate(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        // 获取表单提交的数据
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String url = req.getParameter("url");
        String seq = req.getParameter("seq");
        String parentId = req.getParameter("parentId");
        SysMenu menu = new SysMenu();
        menu.setName(name);
        menu.setUrl(url);
        if(StringUtils.isNotEmpty(seq)){
            menu.setSeq(Integer.parseInt(seq));
        }
        if(StringUtils.isNotEmpty(parentId)){
            menu.setParentId(Integer.parseInt(parentId));
        }
        if(StringUtils.isNotEmpty(id)){
        // 更新
            menu.setId(Integer.parseInt(id));
            service.updteById(menu);
        }else{
        // 添加
            service.save(menu);
            //保存分配的菜单信息 需要获取id

        }
        resp.sendRedirect("/sys/menuServlet?action=list");
    }
    @Override
    public void saveOrUpdatePage(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        //查询所有的父菜单 parentId = -1
        List<SysMenu> list = service.getAllParent();
        req.setAttribute("parents",list);
        //判断是否跟新
        String id = req.getParameter("id");
        if(StringUtils.isNotEmpty(id)){
            SysMenu entity = service.findById(Integer.parseInt(id));
            req.setAttribute("entity",entity);
        }
        req.getRequestDispatcher("/sys/menu/addOrUpdate.jsp").forward(req,resp);
    }
    @Override
    public void remove(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        int id = Integer.parseInt(req.getParameter("id"));
        // 删除菜单的判断
        // 1.菜单如果分配给了角色就不能被删除
        boolean flag = service.isDispatcher(id);
        String msg = "ok";
        if(flag){
        // 表示已经被分配了
            msg = "error";
        }else{
            SysMenu entity = service.findById(id);
            // 2.需要删除的菜单是父菜单
            if(entity.getParentId() == -1){
                // 有子菜单的父菜单不能被删除 -- 判断是否有子菜单
                flag = service.havaSubMenu(id);
                if(flag){
                // 有子菜单
                    msg = "error";
                }else{
                // 父菜单没有子菜单 可以删除
                    service.deleteById(id);
                }
            }else{
                // 3.子菜单 可以被删除
                service.deleteById(id);
            }
        }
        PrintWriter writer = resp.getWriter();
        writer.write(msg);
        writer.flush();
    }
    @Override
    public void findById(HttpServletRequest req, HttpServletResponse resp) throws Exception {

    }
}
